#include "../mtcp_module.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int ReadDataFromFile_float(const char * file, float * buffer, int * count);
int ReadDataFromFile_int(const char * file, int * data, int * count);

VTST::VTST()
{
	m_pdata = nullptr;
	memset(&m_VTST_REQ, 0, sizeof(m_VTST_REQ));
	m_VTST_REQ.FORWARD_I = 1.0;
	m_VTST_REQ.FORWARD_V = 4.0;
	for (size_t i = 0; i < 0; i++)
	{
		m_VTST_REQ.FWHM_WAVELENGTH[i] = i;
		m_VTST_REQ.PEAK_OPTICAL_POWER[i] = i * 2;
		m_VTST_REQ.PEAK_WAVELENGTH[i] = i * 3;
	}
	m_VTST_REQ.NTC_INIT = 25.0;
	m_VTST_REQ.NTC_STEADY_STATE = 26.0;
	m_VTST_REQ.REVERSE_I = 1;
	m_VTST_REQ.REVERSE_V = 4;
}


VTST::~VTST()
{
}


int VTST::NTC_INIT(float v)
{
	m_VTST_REQ.NTC_INIT = v;

	return 0;
}
int VTST::PEAK_OPTICAL_POWER(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_VTST_REQ.PEAK_OPTICAL_POWER[index++] = atof(k);
		if (index > 9) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int VTST::REVERSE_V(float voltage)
{
	m_VTST_REQ.REVERSE_V = voltage;
	return 0;
}
int VTST::REVERSE_I(float current)
{
	m_VTST_REQ.REVERSE_I = current;
	return 0;
}
int VTST::FORWARD_V(float v)
{
	m_VTST_REQ.FORWARD_V = v;
	return 0;
}
int VTST::FORWARD_I(float v)
{
	m_VTST_REQ.FORWARD_I = v;
	return 0;
}
int VTST::NTC_STEADY_STATE(float ntc)
{
	m_VTST_REQ.NTC_STEADY_STATE = ntc;
	return 0;
}
int VTST::T_STEADY_STATE(float time)
{
	m_VTST_REQ.T_STEADY_STATE = time;
	return 0;
}
int VTST::PEAK_WAVELENHTH(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		m_VTST_REQ.PEAK_WAVELENGTH[index] = atof(k);
		printf("Set peak power : [%d] = %f",index,atof(k));
		index++;
		if (index > 9) break;
		k = strtok(NULL, ",");
	}
	return 0;
}

int VTST::FWHM_WAVELENGTH(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		m_VTST_REQ.FWHM_WAVELENGTH[index++] = atof(k);
		if (index > 9) break;
		k = strtok(NULL, ",");
	}
	return 0;
}

int VTST::WL_DATA_CNT(uint8_t v)
{
	m_VTST_REQ.WL_DATA_CNT = v;
	return 0;
}
int VTST::WL_LEN(int v)
{
	m_VTST_REQ.WL_LEN = v;
	return 0;
}

//int setPayload(tMTCP_payload_VTST_REQ payload);
int VTST::pushData(const char * d1, const char * d2)
{
	//do nothing now;
	return 0;
}

int VTST::pushWaveLength(const char * wave)
{
	return 0;
}

int VTST::setvalue(char *what, char* value)
{
	
	if (0 == strcmp(what, "NTC_INIT")){
		NTC_INIT(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "PEAK_OPTICAL_POWER")){
		PEAK_OPTICAL_POWER(value);
		return 0;
	}
	if (0 == strcmp(what, "REVERSE_V")){
		REVERSE_V(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "REVERSE_I")){
		REVERSE_I(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "FORWARD_V")){
		FORWARD_V(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "FORWARD_I")){
		FORWARD_I(atof(value));
		return 0;
	}

	
	if (0 == strcmp(what, "NTC_STEADY_STATE")){
		NTC_STEADY_STATE(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "T_STEADY_STATE")){
		T_STEADY_STATE(atof(value));
		return 0;
	}

	if (0 == strcmp(what, "PEAK_WAVELENHTH")){
		PEAK_WAVELENHTH(value);
		return 0;
	}
	if (0 == strcmp(what, "FWHM_WAVELENGTH")){
		FWHM_WAVELENGTH(value);
		return 0;
	}
	if (0 == strcmp(what, "WL_DATA_CNT")){
		WL_DATA_CNT(atoi(value));
		return 0;
	}
	if (0 == strcmp(what, "WL_LEN")){
		WL_LEN(atof(value));
		return 0;
	}

	return -1;
}

int  VTST::SendVTST()
{
	/*
	//packet raw data;
	const char * wave_file = "C:\\RawData\\vtst_wavelen.dat";
	const char * meas_file = "C:\\RawData\\c1ss_meas.dat";
	float wave[1024];
	float meas[1024];
	int count;
	int ret = ReadDataFromFile_float(wave_file, wave, &count);
	if (ret<0)
	{
		printf("###Read raw dat file failed!");
	}
	ret = ReadDataFromFile_float(meas_file, meas, &count);
	if (ret<0)
	{
		printf("###Read raw dat file failed!");
	}

	tMTCP_data_TTDP * p = new tMTCP_data_TTDP[count];

	for (size_t i = 0; i < count; i++)
	{
		p[i].measure = meas[i];
		p[i].wavelength = wave[i];
	}

	m_VTST_REQ.WL_LEN = count;

	//int ret = SendFrame(kMTCP_CTRL_C1SS, &m_C1SS_REQ, sizeof(m_C1SS_REQ), nullptr, 0);
	ret = SendFrame(kMTCP_CTRL_C1SS, &m_VTST_REQ, sizeof(m_VTST_REQ), p, count * sizeof(tMTCP_data_TTDP));

	delete[]p;

	*/
	int ret = SendFrame(kMTCP_CTRL_VTST, &m_VTST_REQ, sizeof(m_VTST_REQ), m_pdata, sizeof(tMTCP_data_TTDP)*m_VTST_REQ.WL_LEN);

	tMTCP_payload_TEST_RSP rep;
	int recv_cnt = RecvResponse(&rep, sizeof(rep));
	return ret;
}
